//
//  BaseViewController.swift
//  XLWebo
//
//  Created by 陈晓龙 on 17/2/15.
//  Copyright © 2017年 陈晓龙. All rights reserved.
//

import UIKit

class BaseViewController: UIViewController {
    //访客视图信息字典
    var  visitorInfoDict:[String:String]?
    
   //用户是否登录
    var userLogon = false
    
    var tableView:UITableView?
    var refreshControl: UIRefreshControl?
    var isPullup = false
//自定义导航条
    lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    //自定义导航条目，设置导航内容 使用navItem
    lazy var navItem = UINavigationItem()
    
    override func viewDidLoad() {
        super.viewDidLoad()

    setupUI()
    loadData()
        
        
    }
 //加载数据源方法
    func loadData() {
        refreshControl?.endRefreshing()
    }
    
    
     //重写title的setter方法
    override var title: String?{
        didSet {
            navItem.title = title
        }
    }
   
  
    
}

// MARK: 设置监听方法
extension BaseViewController {
    func register() {
        print("zce")
    }
    func login() {
      print("登录")
    }
}
// MARK:设置界面
extension BaseViewController {
    //设置UI
    func setupUI()  {
        view.backgroundColor = UIColor.cz_random()
        //取消自动缩进
        automaticallyAdjustsScrollViewInsets = false
        setupNavigationBar()
        setupTableView()
        //如果没有登录进入访客界面
        userLogon ? setupTableView():visitorView()
        
    }
    
    //设置访客视图
    func visitorView() {
        let vistorView = WBVisitorView(frame: view.bounds)
        //vistorView.backgroundColor = UIColor.cz_random()
       // view.addSubview(vistorView)
        vistorView.visitorInfo = visitorInfoDict
        view.insertSubview(vistorView, belowSubview: navigationBar)
        vistorView.registerButton.addTarget(self, action:#selector(register), for: .touchUpInside)
        vistorView.loginButton.addTarget(self, action: #selector(login), for: .touchUpInside)
    
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(register))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录",style:.plain, target: self, action: #selector(login))
        
    }
    //设置表格 
     func setupTableView() {
        tableView = UITableView(frame: view.bounds, style: .plain)
        //view.addSubview(tableView)
        view.insertSubview(tableView!, belowSubview: navigationBar)
        tableView?.delegate = self
        tableView?.dataSource = self
        //设置内容缩进
        tableView?.contentInset = UIEdgeInsets(top: navigationBar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        //设置刷新控件
        refreshControl = UIRefreshControl()
        tableView?.addSubview(refreshControl!)
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
    //自定义navgiationBar
     func setupNavigationBar() {
        view.addSubview(navigationBar)
        //将item设置给bar
        navigationBar.items = [navItem]
        //设置bar渲染
        navigationBar.barTintColor = UIColor.cz_color(withHex: 0xf6f6f6)//设置导航条整个背景的颜色
        //设置navBar字体颜色
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.darkGray]
        //设置按钮字体
        navigationBar.tintColor = UIColor.orange
    }
}


// MARK:实现协议方法
extension BaseViewController: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    //显示最后一行的时候 上拉刷新
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let row = indexPath.row
        let section = tableView.numberOfSections-1
       // print(row)
        print("section\(section)")
        if row < 0||section < 0 {
            return
        }
        //总行数
        let count = tableView.numberOfRows(inSection: section)
        print("count-->> \(count)")
        if row == (count - 1) && !isPullup {
            print("开始上啦刷新")
             isPullup = true
        loadData()
        }
        
    }
}
